#include "head.h"
// 购票
void buy_ticket (FlightSystem * Fly_list,U_data log_data,U_list * list ,D_list * order)
{
    int flag = 0;
    const wchar_t *title_purchase = L"购票界面";
    const wchar_t *option_purchase[] = {
    L"为自己购票",
    L"为他人购票(必须是在平台注册过的用户)",
    L"返回"};
    while (1)
    {
        flag = display_menu(title_purchase, option_purchase, 3);
        switch (flag)
        {
        case 1:
            my_buy_ticket (Fly_list, log_data , order);  // 为自己购票
            break;
        case 2:
            his_buy_ticket (Fly_list, log_data, list,order); // 为他人购票
            break;
        case 3:
            return ;    
        default:
            break;
        }
    }
}
// 为自己购票
void my_buy_ticket (FlightSystem * Fly_list,U_data log_data ,D_list * order)
{
    char departure[128], destination[128];
    while (1)
    {
        // 用户输入起始地和目的地  
        printf("请输入起始地：");  
        scanf("%s", departure);  
        printf("请输入目的地：");  
        scanf("%s", destination);  
        // 查找并打印符合条件的航班  
        int P = findFlights(Fly_list->Flight_list , departure, destination);
        if (P == 0)
        {
            printf("未找到符合要求的航班\n");
            sleep(2);
        }
        else break;
    }
    // 选择并购买航班
    int flight_id = 0;
    printf("请选择要购买的班次：");
    scanf("%d", &flight_id);
    int dr = Find_Same_Order(order, log_data.name ,flight_id);
    if (dr == 1)
    {
        printf("您已经购买过此航班\n");
        sleep(2);
        return ;
    }
    F_node *ptr = D_List_find_id(Fly_list->Flight_list, flight_id);
    if (ptr!= NULL)
    {
        if (ptr->data.value > log_data.value)
        {
            printf("您的余额不足，无法购买该航班\n"); 
            sleep(2); 
            return;
        }
        if (ptr->data.leftbuyersa<=0)
        {
            printf("sorry,该航班已售完\n");
            sleep(2);  
            return;
        }
        // 检查订单有没有上限 
        if (log_data.numTickets >= 10) 
        {  
            printf("尊敬的用户您的订单已上限,无法添加更多订单\n");
            sleep(2);  
            return;  
        }
        ptr->data.leftbuyersa--;
        D_List_file_print(Fly_list->Flight_list);
        discount(ptr, log_data);    
        printf("班次 %d , 机型 %s 已添加到用户订单中\n", ptr->data.id, ptr->data.flighttype);
        D_data or_data;
        strcpy(or_data.name, log_data.name);
        or_data.ticket = ptr->data;
        or_data.numTickets = log_data.numTickets;
        R_List_insert(order, or_data, 1);
        R_List_File_print(order, "./data/order_member.txt");
        sleep(3);
        log_data.value -= ptr->data.value;
        collect_money_1(Fly_list ,log_data);
    }
    else printf("没找到目标班次\n");
    sleep(2);
    return ;
}
// 为他人购票
void his_buy_ticket (FlightSystem *Fly_list,U_data log_data,U_list * list,D_list * order)
{
    char username[128];
    U_node * user;
    printf("请输入您赠与的用户名：");
    scanf("%s", username);
    user = S_List_find_name(list, username);
    if (user == NULL)
    {
        printf("您所输入的用户名不存在\n");
        sleep(2);
        return ;
    }
    char mima[128];
    printf("请输入该用户的密码：");
    scanf("%s", mima);
    if (strcmp(user->data.passwd, mima)!= 0)
    {
        printf("您所输入的密码不正确\n");
        sleep(2);
        return ;
    }
    char departure[128], destination[128];
    while (1)
    {
        // 用户输入起始地和目的地  
        printf("请输入起始地：");  
        scanf("%s", departure);  
        printf("请输入目的地：");  
        scanf("%s", destination);  
        // 查找并打印符合条件的航班  
        int P = findFlights(Fly_list->Flight_list , departure, destination);
        if (P == 0)
        {
            printf("未找到符合要求的航班\n");
            sleep(2);
        }
        else break;
    }
    // 选择并购买航班
    int flight_id = 0;
    printf("请选择要购买的班次：");
    scanf("%d", &flight_id);
    int dr = Find_Same_Order(order, user->data.name ,flight_id);
    if (dr == 1)
    {
        printf("这位用户已经购买过此航班\n");
        sleep(2);
        return ;
    }
    F_node *ptr = D_List_find_id(Fly_list->Flight_list, flight_id);
    if (ptr!= NULL)
    {
        if (ptr->data.value > log_data.value)
        {
            printf("您的余额不足，无法购买该航班\n");
            sleep(2);  
            return;
        }
        if (ptr->data.leftbuyersa<=0)
        {
            printf("sorry,该航班已售完\n");
            sleep(2);  
            return;
        }
        // 检查订单有没有上限 
        if (user->data.numTickets >= 10) 
        {  
            printf("该用户的订单已上限,无法添加更多订单\n");
            sleep(2);  
            return;  
        }
        ptr->data.leftbuyersa--;
        D_List_file_print(Fly_list->Flight_list);
        discount(ptr, log_data);    
        printf("班次 %d , 机型 %s 已添加到用户订单中\n", ptr->data.id, ptr->data.flighttype);
        sleep(3);
        log_data.value = log_data.value-ptr->data.value;
        D_data or_data;
        strcpy(or_data.name, user->data.name);
        or_data.ticket = ptr->data;
        R_List_insert(order, or_data, 1);
        R_List_File_print(order, "./data/order_member.txt");
        printf("%s\n",user->data.name);
        printf("%d\n",user->data.grade);
        sleep(1);
        Order_change(Fly_list ,user->data);
        collect_money_2(Fly_list ,log_data);
    }
    else printf("没找到目标班次\n");
    sleep(2);
    return ;
}
// 折扣
void discount(F_node * ptr, U_data log_data)
{
    switch (log_data.grade)
    {
    case 1:
        break;
    case 2:
        break;
    case 3:
        ptr->data.value *= SILVER_DISCOUNT;
        break;
    case 4:
        ptr->data.value *= GOLD_DISCOUNT;
        break;
    case 5:
        ptr->data.value *= PLATINUM_DISCOUNT;
        break;
    default:
        break;
    }
}
// 收钱
void collect_money_1 (FlightSystem * Fly_list ,U_data log_data)
{
    U_node * ptr1;
    U_node * ptr2;
    U_node * ptr3;
    U_node * ptr4;
    U_node * ptr5;
    switch (log_data.grade)
    {
    case 1:
        ptr1= S_List_find_name(Fly_list->ADMIN_USER , log_data.name);
        ptr1->data.value = log_data.value;
        ptr1->data.numTickets++;
        S_List_File_print(Fly_list->ADMIN_USER,ADMIN_FILE);
        break;
    case 2:
        ptr2= S_List_find_name(Fly_list->REGULAR_USER , log_data.name);
        ptr2->data.value = log_data.value;
        ptr2->data.numTickets++;
        S_List_File_print(Fly_list->REGULAR_USER,REGULAR_FILE);
        break;
    case 3:
        ptr3= S_List_find_name(Fly_list->SILVER_USER , log_data.name);
        ptr3->data.value = log_data.value;
        ptr3->data.numTickets++;
        S_List_File_print(Fly_list->SILVER_USER,SILVER_FILE);
        break;
    case 4:
        ptr4= S_List_find_name(Fly_list->GOLD_USER , log_data.name);
        ptr4->data.value = log_data.value;
        ptr4->data.numTickets++;
        S_List_File_print(Fly_list->GOLD_USER,GOLD_FILE);
        break;
    case 5:
        ptr5= S_List_find_name(Fly_list->PLATINUM_USER , log_data.name);
        ptr5->data.value = log_data.value;
        ptr5->data.numTickets++;
        S_List_File_print(Fly_list->PLATINUM_USER,PLATINUM_FILE);
        break;
    default:
        break;
    }
}
void collect_money_2 (FlightSystem * Fly_list ,U_data log_data)
{
    U_node * ptr1;
    U_node * ptr2;
    U_node * ptr3;
    U_node * ptr4;
    U_node * ptr5;
    switch (log_data.grade)
    {
    case 1:
        ptr1= S_List_find_name(Fly_list->ADMIN_USER , log_data.name);
        ptr1->data.value = log_data.value;
        S_List_File_print(Fly_list->ADMIN_USER,ADMIN_FILE);
        break;
    case 2:
        ptr2= S_List_find_name(Fly_list->REGULAR_USER , log_data.name);
        ptr2->data.value = log_data.value;
        S_List_File_print(Fly_list->REGULAR_USER,REGULAR_FILE);
        break;
    case 3:
        ptr3= S_List_find_name(Fly_list->SILVER_USER , log_data.name);
        ptr3->data.value = log_data.value;
        S_List_File_print(Fly_list->SILVER_USER,SILVER_FILE);
        break;
    case 4:
        ptr4= S_List_find_name(Fly_list->GOLD_USER , log_data.name);
        ptr4->data.value = log_data.value;
        S_List_File_print(Fly_list->GOLD_USER,GOLD_FILE);
        break;
    case 5:
        ptr5= S_List_find_name(Fly_list->PLATINUM_USER , log_data.name);
        ptr5->data.value = log_data.value;
        S_List_File_print(Fly_list->PLATINUM_USER,PLATINUM_FILE);
        break;
    default:
        break;
    }
}
// 增加储存订单的数量
void Order_change (FlightSystem * Fly_list ,U_data user)
{
    U_node * ptr1;
    U_node * ptr2;
    U_node * ptr3;
    U_node * ptr4;
    U_node * ptr5;
    switch (user.grade)
    {
    case 1:
        ptr1= S_List_find_name(Fly_list->ADMIN_USER , user.name);
        ptr1->data.numTickets++;
        S_List_File_print(Fly_list->ADMIN_USER,ADMIN_FILE);
        break;
    case 2:
        ptr2= S_List_find_name(Fly_list->REGULAR_USER , user.name);
        ptr2->data.numTickets++;
        S_List_File_print(Fly_list->REGULAR_USER,REGULAR_FILE);
        break; 
    case 3:
        ptr3= S_List_find_name(Fly_list->SILVER_USER , user.name);
        ptr3->data.numTickets++;
        S_List_File_print(Fly_list->SILVER_USER,SILVER_FILE);
        break;
    case 4:
        ptr4= S_List_find_name(Fly_list->GOLD_USER , user.name);
        ptr4->data.numTickets++;
        S_List_File_print(Fly_list->GOLD_USER,GOLD_FILE);
        break;
    case 5:
        ptr5= S_List_find_name(Fly_list->PLATINUM_USER , user.name);
        ptr5->data.numTickets++;
        S_List_File_print(Fly_list->PLATINUM_USER,PLATINUM_FILE);
        break;
    default:
        break;
    }
}